﻿using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
using MM.Commmon.UI;
using MM.Common.Base;
using MM.Common.DBBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmModuleAdd : FrmBase
    {
        public FrmModuleAdd()
        {
            InitializeComponent();
            this.dtFunction = new DataTable();
            this.dtFunction.Columns.Add("FUNCTION_CODE");
            this.dtFunction.Columns.Add("FUNCTION_NAME");
            this.dtFunction.Columns.Add("OBJECT_NAME");
            this.dtFunction.Columns.Add("PARAMETER");
            this.dtFunction.Columns.Add("USED_FLAG");
            this.gvFunction.GridControl.DataSource = this.dtFunction;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.Cancel;
            base.Close();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog
            {
                Title = "选择模块文件",
                InitialDirectory = Directory.GetCurrentDirectory() + @"\",
                Filter = "动态文件 (*.dll)|*.dll",
                FilterIndex = 1,
                RestoreDirectory = true
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string text = this.txtModuleCode.Text;
                string fileName = dialog.FileName;
                try
                {
                    Assembly assembly = Assembly.LoadFrom(fileName);
                    Type[] types = assembly.GetTypes();
                    fileName = fileName.Substring(0, fileName.Length - 4);
                    int length = fileName.LastIndexOf(@"\");
                    fileName = fileName.Substring(length + 1);
                    for (TreeListNode node = this.moduleNode.Nodes.FirstNode; node != null; node = node.NextNode)
                    {
                        if (node.GetValue("OBJECT_NAME").ToString() == fileName)
                        {
                            XtraMessageBox.Show("已经存在该模块, 不能再加入!", "提示");
                            return;
                        }
                    }
                    this.txtModuleFileName.Text = fileName;
                    this.txtFileTime.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm");
                    object[] customAttributes = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), true);
                    this.txtModuleName.Text = ((AssemblyTitleAttribute)customAttributes[0]).Title;
                    string fullName = assembly.FullName;
                    length = fullName.IndexOf("Version=");
                    fullName = fullName.Substring(length + 8);
                    length = fullName.IndexOf(",");
                    this.txtVersion.Text = fullName.Substring(0, length);
                    this.dtFunction.Rows.Clear();
                    int num2 = 1;
                    foreach (Type type in types)
                    {
                        if (type.IsClass)
                        {
                            bool flag = false;
                            for (Type type2 = type.BaseType; type2 != null; type2 = type2.BaseType)
                            {
                                if (type2.Name == "FrmFunctionBase")
                                {
                                    flag = true;
                                    break;
                                }
                            }
                            if (flag)
                            {
                                string str4 = "";
                                string formCode = text + "-W" + num2.ToString("00");
                                try
                                {
                                    FrmFunctionBase base2 = (FrmFunctionBase)Assembly.LoadFrom(type.Module.ToString()).CreateInstance(type.FullName);
                                    str4 = base2.Text;
                                    if (!string.IsNullOrEmpty(base2.FormCode))
                                    {
                                        formCode = base2.FormCode;
                                    }
                                }
                                catch (Exception exception)
                                {
                                    MessageBox.Show("获取窗口：" + type.Name + " 出错！\r\n" + exception.Message, "提示");
                                }
                                DataRow row = this.dtFunction.NewRow();
                                row["FUNCTION_CODE"] = formCode;
                                row["FUNCTION_NAME"] = str4;
                                row["OBJECT_NAME"] = type.Name;
                                row["USED_FLAG"] = "Y";
                                this.dtFunction.Rows.Add(row);
                                num2++;
                            }
                        }
                    }
                }
                catch (Exception exception2)
                {
                    MessageBox.Show("出错：" + exception2.Message, "提示");
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            for (int i = this.dtFunction.Rows.Count - 1; i >= 0; i--)
            {
                if (this.dtFunction.Rows[i]["USED_FLAG"].ToString() != "Y")
                {
                    this.dtFunction.Rows.Remove(this.dtFunction.Rows[i]);
                }
            }
            string text = this.txtModuleCode.Text;
            string paramValue = this.txtModuleName.Text;
            string str3 = this.txtModuleFileName.Text;
            string str4 = "N";
            if (this.ckIsValid.Checked)
            {
                str4 = "Y";
            }
            string[] commandtexts = new string[this.dtFunction.Rows.Count + 1];
            DataParamCol[] parms = new DataParamCol[this.dtFunction.Rows.Count + 1];
            commandtexts[0] = "INSERT INTO MXHLY_SYS_MODULE (MODULE_CODE ,MODULE_NAME ,VERSION ,MODULE_FILENAME , FILE_TIME ,SERIAL ,UPDATE_TIME ,UPDATE_EMP_ID ,USED_FLAG) \r\n VALUES (:modulecode ,:modulename ,:version ,:mfilename ,:filetime ,:serial ,SYSDATE ,:operid ,:isvalid) ";
            parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("modulecode", text), new InputDataParam("modulename", paramValue), new InputDataParam("version", this.txtVersion.Text), new InputDataParam("mfilename", str3), new InputDataParam("filetime", this.txtFileTime.Text), new InputDataParam("serial", this.txtSerial.Text), new InputDataParam("operid", SystemInfo.OperId), new InputDataParam("isvalid", str4) });
            for (int j = 0; j < this.dtFunction.Rows.Count; j++)
            {
                commandtexts[j + 1] = "INSERT INTO MXHLY_SYS_FUNCTION (FUNCTION_CODE ,FUNCTION_NAME ,OBJECT_NAME ,PARAMETER ,MODULE_CODE ,USED_FLAG) \r\n   VALUES (:functioncode ,:functionname ,:objectname ,:parameter ,:modulecode ,:isvalid)";
                parms[j + 1] = new DataParamCol(new DataParam[] { new InputDataParam("functioncode", this.dtFunction.Rows[j]["FUNCTION_CODE"]), new InputDataParam("functionname", this.dtFunction.Rows[j]["FUNCTION_NAME"]), new InputDataParam("objectname", this.dtFunction.Rows[j]["OBJECT_NAME"]), new InputDataParam("parameter", this.dtFunction.Rows[j]["PARAMETER"]), new InputDataParam("modulecode", text), new InputDataParam("isvalid", "Y") });
            }
            if (SystemInfo.DbData.RunSql(commandtexts, parms) == 0)
            {
                TreeListNode parentNode = this.moduleNode.TreeList.AppendNode(new object[] { text, paramValue, str3, "", this.ckIsValid.Checked ? "Y" : "N", "" }, this.moduleNode);
                parentNode.ImageIndex = 1;
                parentNode.SelectImageIndex = 1;
                this.moduleNode.TreeList.FocusedNode = parentNode;
                for (int k = 0; k < this.dtFunction.Rows.Count; k++)
                {
                    string str5 = this.dtFunction.Rows[k]["FUNCTION_CODE"].ToString();
                    string str6 = this.dtFunction.Rows[k]["FUNCTION_NAME"].ToString();
                    string str7 = this.dtFunction.Rows[k]["OBJECT_NAME"].ToString();
                    string str8 = this.dtFunction.Rows[k]["PARAMETER"].ToString();
                    TreeListNode node2 = parentNode.TreeList.AppendNode(new object[] { str5, str6, str7, str8, "Y", "" }, parentNode);
                    node2.ImageIndex = 2;
                    node2.SelectImageIndex = 2;
                }
                base.DialogResult = DialogResult.OK;
                base.Close();
            }
            else
            {
                XtraMessageBox.Show("更新不成功！\n原因：" + SystemInfo.DbData.ErrorMessage, "出错", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        public bool SetParameter(TreeListNode node)
        {
            if (this.Init())
            {
                this.moduleNode = node;
                return true;
            }
            return false;
        }

        private void txtModuleCode_TextChanged(object sender, EventArgs e)
        {
            string text = this.txtModuleCode.Text;
            for (int i = 0; i < this.dtFunction.Rows.Count; i++)
            {
                string str2 = this.dtFunction.Rows[i]["FUNCTION_CODE"].ToString();
                if (str2.Substring(0, 1) == "-")
                {
                    this.dtFunction.Rows[i]["FUNCTION_CODE"] = text + str2;
                }
            }
        }
    }
}
